﻿using System;
using System.Windows.Forms;
using System.Messaging;

using Harlinn.Messaging.Common;

namespace Harlinn.Messaging.Server
{
    public partial class ServerMainForm : Form
    {
        private MessageQueue messageQueue;
        private bool isRunning;

        public ServerMainForm()
        {
            InitializeComponent();
            InitializeQueue();
        }

        private void InitializeQueue()
        {
            receivedCounter = 0;
            string queuePath = Constants.QueueName;
            if (!MessageQueue.Exists(queuePath))
            {
                messageQueue = MessageQueue.Create(queuePath);
            }
            else
            {
                messageQueue = new MessageQueue(queuePath);
            }
            isRunning = true;
            messageQueue.Formatter = new BinaryMessageFormatter();
            messageQueue.ReceiveCompleted += OnReceiveCompleted;
            messageQueue.BeginReceive();
        }

        private delegate void LogMessageDelegate(string text);
        private void LogMessage(string text)
        {
            if (InvokeRequired)
            {
                Invoke(new LogMessageDelegate(LogMessage), text);
            }
            else
            {
                messageTextBox.AppendText(text + Environment.NewLine);
            }
        }

        private int receivedCounter;
        private void OnReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult)
        {
            try
            {
                MessageQueue mq = (MessageQueue)source;

                if (mq != null)
                {
                    try
                    {
                        System.Messaging.Message message = null;
                        try
                        {
                            message = mq.EndReceive(asyncResult.AsyncResult);
                        }
                        catch (Exception ex)
                        {
                            LogMessage(ex.Message);
                        }
                        if (message != null)
                        {
                            Payload payload = message.Body as Payload;
                            if (payload != null)
                            {
                                receivedCounter++;
                                if ((receivedCounter % 10000) == 0)
                                {
                                    string messageText = string.Format("Received {0} messages", receivedCounter);
                                    LogMessage(messageText);
                                }
                            }
                        }
                    }
                    finally
                    {
                        if (isRunning)
                        {
                            mq.BeginReceive();
                        }
                    }
                }
                return;
            }
            catch (Exception exc)
            {
                LogMessage(exc.Message);
            }
        }


    }
}
